<?php

class Adept_Db_Query_Update extends Adept_Db_Query_Conditional
{

    protected $table;
    protected $set;

    const NEW_LINE = "\n\t";

    public function __toString()
    {
        if (!is_null($this->table)) {
            $sql = 'UPDATE ' . $this->table;
        } else {
            throw new Adept_Db_Query_Exception('Nothing to update. Table not defined');
        }

        if (count($this->set) > 0) {
            $sql .= self::NEW_LINE . 'SET ' . implode(', ', $this->set);
        } else {
            throw new Adept_Db_Query_Exception('Nothing parameters to SET');
        }

        if (count($this->where) > 0) {
            $sql .= self::NEW_LINE . 'WHERE ' . implode(' ', $this->where);
        }
        return $sql;
    }

    public function table($table)
    {
        $this->table = "`{$table}`";
        return $this;
    }

    public function bind($bind)
    {
        $this->value($bind, ":{$bind}:");
        return $this;
    }

    public function binds($binds)
    {
        foreach ($binds as $bind) {
            $this->bind($bind);
        }
        return $this;
    }

    public function value($key, $value)
    {
        $this->set[] = "`{$key}` = {$value}";
        return $this;
    }

    public function values($values)
    {
        foreach ($values as $key => $value) {
            $this->value($key, $value);
        }
        return $this;
    }

}